<template>
  <view class="box">
    <u-navbar>
      <template #left> 首页 </template>
    </u-navbar>

    <view>
      当前
      <view>{{ isLogin ? "已登录" : "未登录" }}</view>
      <view>{{ isFirstUse ? "第一次使用" : "非第一次使用" }}</view>
    </view>

    <u-button text="去登录页面" type="success" @click="toLoginPage"></u-button>

    <u-button
      text="获取用户信息"
      type="primary"
      @click="toGetUserInfo"
    ></u-button>

    <u-button
      text="更改用户昵称"
      type="error"
      @click="changeNickname"
    ></u-button>

    <u-button
      text="去外链页面"
      type="warning"
      @click="toWebviewPage"
    ></u-button>

    <view>{{ userInfo }}</view>
    <u-divider></u-divider>
    <m-upload height="200" width="200" multiple uploadText="上传中"></m-upload>
  </view>
</template>
<script lang="ts" setup>
import { useUserStore, useAppStore } from "@/store";
import { computed } from "vue";
import { editUserNickname } from "@/api";

const userStore = useUserStore();

const appStore = useAppStore();

const userInfo = computed(() => userStore.userInfo);

const isLogin = computed(() => userStore.isLogin);

const isFirstUse = computed(() => appStore.isFirstUse);

const toLoginPage = () => {
  uni.redirectTo({
    url: `/pages/login/index?from=${encodeURIComponent("/pages/index/index")}`,
    success: (res) => {
      console.log("navigateTo success", res);
    },
    fail: (err) => {
      console.log("navigateTo fail", err);
    },
    complete: () => {
      console.log("navigateTo complete");
    },
  });
};

const toGetUserInfo = async () => {
  const res = userStore.getInfo();
  console.log(41, res);
};

const changeNickname = async () => {
  const res = await editUserNickname({
    nickname: `老明${Math.random()}`.substring(0, 5),
  });
  console.log(res);
  toGetUserInfo();
};

const toWebviewPage = () => {
  uni.navigateTo({
    url: `/pages/webview/index?src=${encodeURIComponent(
      "http://localhost:8888/",
    )}`,
  });
};
</script>

<style lang="scss" scoped>
.box {
  padding-top: var(--status-bar-height);
}
</style>
